package com.toman.ui.work.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.CardView;
import android.util.Log;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.toman.Adpter.ItemListMoreAdapter;
import com.toman.Interfaces.NewBaseUrl;
import com.toman.JavaBean.BeltlineEntity;
import com.toman.R;
import com.toman.Util.ViewUtil;
import com.toman.widget.MyListView;
import com.toman.Util.StringUtils;
import com.toman.Util.ToastUtil;
import com.toman.bean.ItemListBean;
import com.toman.toolbar.ToolBarActivity;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Request;
import okhttp3.Response;

public class BeltlineInfActivity extends ToolBarActivity {
    String id;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_company_id)
    TextView tvCompanyId;
    @BindView(R.id.tv_title2)
    TextView tvTitle2;
    @BindView(R.id.tv_title3)
    TextView tvTitle3;
    @BindView(R.id.tv_title4)
    TextView tvTitle4;
    @BindView(R.id.tv_title5)
    TextView tvTitle5;
    @BindView(R.id.cv_base)
    CardView cvBase;
    @BindView(R.id.tv_content1)
    TextView tvContent1;
    @BindView(R.id.lv1)
    MyListView lv1;
    @BindView(R.id.tv_content2)
    TextView tvContent2;
    @BindView(R.id.lv2)
    MyListView lv2;
    @BindView(R.id.cv_more)
    CardView cvMore;
    ItemListMoreAdapter adapter1;
    ItemListMoreAdapter adapter2;
    List<ItemListBean> list1 = new ArrayList<ItemListBean>();
    List<ItemListBean> list2 = new ArrayList<ItemListBean>();
    @BindView(R.id.tv_title6)
    TextView tvTitle6;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_beltline_inf);
        ButterKnife.bind(this);
        initToolBar("产线详情");
        initView();
        get();
    }

    private void initView() {
        id = getIntent().getStringExtra("id");
        adapter1 = new ItemListMoreAdapter(this);
        adapter2 = new ItemListMoreAdapter(this);
        lv1.setAdapter(adapter1);
        lv2.setAdapter(adapter2);

//initList();
    }

    private void initList() {
        List<ItemListBean> list = new ArrayList<ItemListBean>();
        String items[] = new String[]{"产线产量图", "产线OEE图", "产量推移图", "类型产量图"};
        for (int i = 0; i < items.length; i++) {
            ItemListBean bean = new ItemListBean(items[i], i + "");
            list.add(bean);
        }
        adapter1.refreshData(list);
        adapter2.refreshData(list);
    }

    public void get() {
        OkHttpUtils
                .get(NewBaseUrl.beltline_info_get + id)
                .params("token", token)
                .params("id", id)
                .execute(new StringCallback() {
                    @Override
                    public void onResponse(boolean isFromCache, String result, Request request, @Nullable Response response) {
                        Log.i("test", "产线详情: " + result);
                        JSONObject jsonObj = JSON.parseObject(result);
                        int code = jsonObj.getInteger("code");ViewUtil.lagout(code,getApplicationContext());
                        if (code == 0) {
                            BeltlineEntity bean = (BeltlineEntity) JSON.parseObject(jsonObj.getString("beltline"), BeltlineEntity.class);
                            tvTitle.setText(bean.getBeltlineName());
                            tvCompanyId.setText(bean.getBeltlineCode());
                            tvTitle2.setText(bean.getDepartmentName());
                            tvTitle3.setText(bean.getStandardOutput());
                            tvTitle4.setText(bean.getMaxOutput());
                            tvTitle5.setText(bean.getPlanOutput());

                            String items[] = new String[]{"正常", "维修", "废弃", "未投产"};
                            if (!StringUtils.isEmpty(bean.getBeltlineStatus())) {
                                tvTitle6.setText(items[Integer.parseInt(bean.getBeltlineStatus())]);
                            }

                            try {
                                String[] operatorUserName = bean.getOperatorUserName().split(",");
                                String[] operatorUserID = bean.getOperatorUserNum().split(",");
                                String[] MaintenanceUserName = bean.getMaintenanceUserName().split(",");
                                String[] MaintenanceUserID = bean.getMaintenanceUserNum().split(",");
                                for (int i = 0; i < operatorUserName.length; i++) {
                                    list1.add(new ItemListBean(operatorUserName[i], operatorUserID[i]));
                                }
                                for (int i = 0; i < MaintenanceUserName.length; i++) {
                                    list2.add(new ItemListBean(MaintenanceUserName[i], MaintenanceUserID[i]));

                                }
                                adapter1.refreshData(list1);
                                adapter2.refreshData(list2);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        } else {
                            ToastUtil.error(getApplicationContext(), jsonObj.getString("msg"));
                        }

                    }
                });
    }
}
